package com.smalls.newvideotwo.mvp.presenter;

import com.smalls.newvideotwo.NewDTVApp;
import com.smalls.newvideotwo.mvp.base.BaseBeanResult;
import com.smalls.newvideotwo.mvp.base.BasePresenter;
import com.smalls.newvideotwo.mvp.beanResult.GetHotMovieResult;
import com.smalls.newvideotwo.mvp.http.constants.PortalErr;
import com.smalls.newvideotwo.mvp.http.constants.PortalErrUtil;
import com.smalls.newvideotwo.mvp.model.GetAllMovieModel;
import com.smalls.newvideotwo.mvp.model.GetHotMovieModel;
import com.smalls.newvideotwo.mvp.view.IGetHotMovieView;
import com.smalls.newvideotwo.util.LogUtil;
import com.smalls.newvideotwo.utils.Constant;

import java.net.ConnectException;
import java.net.SocketTimeoutException;
import java.util.concurrent.TimeoutException;

/**
 * Created by hs on 2018/3/20.
 */

public class GetHotMoviePresenter extends BasePresenter<IGetHotMovieView> implements BaseBeanResult.IGetHotMovie {
    private static String TAG = Constant.baddcoolio2tag? Constant.TAGPREFIXSTR + "GetHotMoviePresenter":"GetHotMoviePresenter";
    private com.smalls.newvideotwo.mvp.model.modelInterface.IGetHotMovie model = null;
    private boolean isLive;
    private int start = 0;
    private int end = 0;

    public GetHotMoviePresenter(IGetHotMovieView view,int type) {
        super(view);
        if(type==0) {
            model = new GetHotMovieModel();
        } else if(type==1){
            model = new GetAllMovieModel();
        }
    }

    public void Abort() {
        if(model!=null) {
            model.Abort();
        }
    }
    public void request(boolean z,int start,int end) {
        this.start = start;
        this.end = end;
        if(model!=null) {
            model.getHotMovie(this,start,end);
        }
        isLive = z;
    }

    @Override
    public void onError(Throwable e) {
        e.printStackTrace();
        String errCode = "";
        if ((e instanceof TimeoutException) || (e instanceof ConnectException) || (e instanceof SocketTimeoutException)||(e instanceof java.net.UnknownHostException)) {
            LogUtil.e(this.TAG, "\u663e\u793aDialog\u4fe1\u606fEB2");
            errCode = PortalErr.STR_BOX_EPG_TIMEOUT_ERROR;

            if (!NewDTVApp.getApp().isLastServer()) {
                NewDTVApp.getApp().setmEpgServerNext();
                request( false,start,end);
                return ;
            }
        } else {
            LogUtil.e(this.TAG, "\u663e\u793aDialog\u4fe1\u606fEB3");
            errCode = PortalErr.PORTAL_SYSTEM;
        }
        PortalErrUtil.getInstance().setErrCode(errCode);
        ((IGetHotMovieView) this.view).showErrDialogGetHotMovie(errCode);
    }

    @Override
    public void onNext(GetHotMovieResult result) {
        PortalErrUtil.getInstance().setErrCode("");
        if (result == null
                || result.getData()==null
                ||result.getData().size() < 0) {
            LogUtil.e(this.TAG, "\u663e\u793aDialog\u4fe1\u606fEB1");
            PortalErrUtil.getInstance().setErrCode(PortalErr.PORTAL_ROOT_COLUMN);
            ((IGetHotMovieView) this.view).showErrDialogGetHotMovie(PortalErr.PORTAL_ROOT_COLUMN);
        } else {
            LogUtil.d(this.TAG, this.TAG + "  onNext result=" + result.toString());
            ((IGetHotMovieView) this.view).onSucceedGetHotMovie(result);
        }
    }
}
